<?php
/**
 * User create
 *
 * @author Mario Schillermann <info@rockit-flash.com>
 * @encoding UTF-8 äöüßÖÄÜ
 * @copyright © 2011 Mario Schillermann (info@rockit-flash.com)
 * @license GNU GPL v2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 * @package Page
 * @subpackage Index
 * @link $HeadURL$
 * @version SVN: $Id$
 * @phpVersion >=5
 * @todo prüfen ob bereits ein user mit dem namen existiert
 */
class User_CreateController extends Zend_Controller_Action
{
    /**
     * Initialisation
     *
     * @return void
     */
    public function init()
    {

    }
    /**
     * Show create form
     *
     * @return void
     */
    public function indexAction()
    {
        try {
            $this->setCss('window');
            $this->setCss('create');

            $layout = Zend_Layout::getMvcInstance();
            $layout->appTab = User_Model_Tab::render($this->view);

            $database = new User_Model_Database;
            $roles = $database->getRoles();
        
            $form = new User_Form_Create;
            $buildForm = $form->build($roles);

            $request = $this->_request->getPost();

            if($request) {

                if($buildForm->isValid($request)) {
                    $this->_forward('create');
                }
            }
            $this->view->form = $buildForm;

        } catch (Exception $e) {

            $this->_forward('app', 'error', 'error', array('log' => $e));
        }
    }
    /**
     * User create
     *
     * @throws Exception
     * @return void
     */
    public function createAction() {
        try {
            $newUser = $this->_request->getPost();

            if($newUser['password'] != $newUser['passwordRepeat']) {

                $this->_forward(
                    'app',
                    'error',
                    'error',
                    array(
                        'message' => 'Passwörter stimmen nicht überein!',
                        'back' => array(
                            'module' => 'rockitbos',
                            'app' => 'user',
                            'tab' => 'create')));
            } else {

                $database = new User_Model_Database;

                if($database->saveNewUser($newUser) === FALSE) {

                    $this->_forward(
                        'app',
                        'error',
                        'error',
                    array(
                        'message' => 'Benutzer <b>' . $newUser['user'] .
                        '</b> konnte nicht gespeichert werden!',
                        'back' => array(
                            'module' => 'rockitbos',
                            'app' => 'user',
                            'tab' => 'create')));
                }
                $this->view->user = $newUser['user'];
            }
        } catch (Exception $e) {

            $this->_forward(
                'error',
                null,
                null,
                array('errorMessage' => $e->getMessage())
            );
        }
    }
    /**
     * Error output
     *
     * @return void
     */
    public function errorAction() {

        $this->setCss('error');
        $this->view->errorMessage = $this->_request->errorMessage;
    }
    protected function setCss($file) {

        $this->view
             ->headLink()
             ->appendStylesheet(
                 DIRECTORY_SEPARATOR . 'user' .
                 DIRECTORY_SEPARATOR . 'css' .
                 DIRECTORY_SEPARATOR . $file . ".css"
               );
    }
}